package com.example.jiemian;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.jiemian.pojo.Sensor;

import java.util.ArrayList;
import java.util.List;

public class ceshi2 extends Fragment {
    //定义最外层表格对象
    private GridView gridView;
    //定义一个数据
    public String[] title=new String[]{"监测1","监测2","监测3","监测4"};

    //声明一个实体集合
    private ArrayList<Sensor> list=new ArrayList<>();

    private MyAdpter myAdpter;

    //获取主框架的GrideView控件页面并设置数据
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle){
        View view =layoutInflater.inflate(R.layout.ceshi2,gridView,false);
        //获取grivade控件
        gridView=view.findViewById(R.id.ceshi2_gridview);
        //清空集合
        list.clear();
        //给集合添加数据
        list.add(new Sensor("空气温度","度",12.9));
        list.add(new Sensor("土壤湿度","百分之",22.9));
        list.add(new Sensor("co2","ennn",32.9));
        list.add(new Sensor("光强","klx",42.9));

        //实例化适配器并传入数据
        myAdpter=new MyAdpter(title);

        gridView.setAdapter(myAdpter);

        return view;
    }

    //绑定数据的适配器
    public class MyAdpter extends BaseAdapter{

       private String[] title1;

        //通过构造函数，将外界的值与内部变量进行交互
        public MyAdpter(String[] title) {
            this.title1 = title;
        }
        @Override
            public int getCount(){
                return this.title1.length;
        }
        @Override
        public Object getItem(int position){
            return null;
        }
        @Override
        public long getItemId(int i){
            return 0;
        }

        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            //设置并获取子页面的视图对象
            View view =LayoutInflater.from(getActivity()).inflate(R.layout.ceshi2_item_view,null);

            //获取页面当中可改变标题的标题控件
            TextView textView0=(TextView)view.findViewById(R.id.ceshi2_textview_item_0);
            TextView textView1=(TextView)view.findViewById(R.id.ceshi2_textview_item_1);
            TextView textView2=(TextView)view.findViewById(R.id.ceshi2_textview_item_2);
            TextView textView3=(TextView)view.findViewById(R.id.ceshi2_textview_item_3);
            TextView textView4=(TextView)view.findViewById(R.id.ceshi2_textview_item_4);

            //从集合当中获取数据并绑定
            String sensorName=list.get(position).getSensorName();//获取集合当中sensorname当中设置的值
            double date=list.get(position).getDate();
            String dataUnit=list.get(position).getDateUnit();
            //通过值来判断显示不同颜色
            if(date>30){
                textView1.setText("报警");
                textView4.setTextColor(getResources().getColor(R.color.red));
                textView0.setBackgroundResource(R.drawable.textview_cicle_red);
            }else {
                textView1.setText("正常");
                textView4.setTextColor(getResources().getColor(R.color.gray));
                textView0.setBackgroundResource(R.drawable.textview_cicle_white);
            }
            String a;
            a= String.valueOf(date);
            textView2.setText(a);
            textView3.setText(dataUnit);
            textView4.setText(sensorName);
            return view;
        }

    }
}
